package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompTMMcFundInfoMapper;
import cc.rengu.igas.bomp.common.entity.BompTMMcFundInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销活动打款记录表数据库操作接口实现
 * Copy By [momp-TMMcFundInfoMapperImpl]
 */

public class BompTMMcFundInfoMapperImpl implements BompTMMcFundInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 新增单条商户成功打款数据数据库操作接口实现
     *
     * @param tmMcFundInfo 营销活动打款记录表数据库操作实体类
     * @return int 返回
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 16:00
     */
    @Override
    public int insertSingleRecordForMchntFundInfo(BompTMMcFundInfo tmMcFundInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompInsertSingleRecordForMchntFundInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcFundInfo, BompTMMcFundInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>营销活动编号<{}>的<{}>的打款资金数据失败!<{}>", tmMcFundInfo.getInstId(), tmMcFundInfo.getMcNo(), tmMcFundInfo.getPayerAcctId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    // /**
    //  * 取当前数据库最大记录顺序号数据库操作接口实现
    //  * 如果有记录,返回值大于等于0
    //  * 如果无数据或失败,返回值小于0;
    //  *
    //  * @param tmMcFundInfo 营销活动打款记录表数据库操作实体类
    //  * @return int 当前数据库最大记录顺序号
    //  * @throws Exception 异常信息
    //  * @author Jinan Liu
    //  * @date 2020/7/8 17:39
    //  */
    // @Override
    // public int checkMaxmiumRecordIndexInfo(BompTMMcFundInfo tmMcFundInfo) throws Exception {
    //
    //     XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
    //     String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
    //     if (null != corporation && !corporation.isEmpty()) {
    //         String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
    //         dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
    //     }
    //     rglog.info("DBPoolName=[{}]", dbPoolName);
    //
    //     DbsUtil dbsUtil = new DbsUtil(dbPoolName);
    //     String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompCheckMaxmiumRecordIndexInfo");
    //
    //     String instId = tmMcFundInfo.getInstId();
    //     String mcNo = tmMcFundInfo.getMcNo();
    //     String payerId = tmMcFundInfo.getPayerAcctId();
    //
    //     int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcFundInfo, tmMcFundInfo);
    //     if (Database.DBS_SUCCESS != iReturnCode) {
    //         if (Database.DBS_NOT_FOUND == iReturnCode || Database.DBS_FETCH_NULL == iReturnCode) {
    //             return -1;
    //         } else {
    //             rglog.error("插入法人机构号<{}>营销活动编号<{}>的<{}>的打款资金数据失败!<{}>", instId, mcNo, payerId, iReturnCode);
    //             /* 需要确保返回负数 */
    //             if (0 >= Integer.signum(iReturnCode)) {
    //                 return iReturnCode;
    //             } else {
    //                 return -1 * iReturnCode;
    //             }
    //         }
    //     }
    //
    //     return Integer.parseInt(tmMcFundInfo.getRecordIndex());
    // }

    @Override
    public int updateTMMcFundInfo(BompTMMcFundInfo tmMcFundInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompUpdateTMMcFundInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcFundInfo, BompTMMcFundInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>营销活动编号<{}>的<{}>的打款资金数据失败!<{}>", tmMcFundInfo.getInstId(), tmMcFundInfo.getMcNo(), tmMcFundInfo.getPayerAcctId(), iReturnCode);
            return iReturnCode;
        }
        return Database.DBS_SUCCESS;
    }

    @Override
    public List<BompTMMcFundInfo> selectBompTMMcFundInfoByInstIdAndMcNoAndDescByCreateTime(String instId, String mcNo, String payerAcctId, String payerAcctType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "bompSelectBompTMMcFundInfoByMcNoAnd");

        BompTMMcFundInfo bompTMMcFundInfo = new BompTMMcFundInfo();
        bompTMMcFundInfo.setInstId(instId);
        bompTMMcFundInfo.setMcNo(mcNo);
        bompTMMcFundInfo.setPayerAcctId(payerAcctId);
        bompTMMcFundInfo.setPayerAcctType(payerAcctType);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bompTMMcFundInfo, BompTMMcFundInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构号和营销活动编号查询营销活动打款记录失败,instId<{}>,mcNo<{}>", instId, mcNo);
            return null;
        }
        return objectList.stream().map(item -> (BompTMMcFundInfo) item).collect(Collectors.toList());
    }
}
